using System;
using Microsoft.SPOT;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;
using System.Threading;
using NetMf.CommonExtensions;
using System.Text;
using System.IO;

namespace FEZ_Panda_II_Application1
{
    public class Sensors
    {
        #region Acceleration Inputs
        AnalogIn x18G = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An0);
        AnalogIn y18G = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An1);
        AnalogIn x5G = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An2);
        AnalogIn y5G = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An3);
        AnalogIn z250G = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An4);
        AnalogIn z18G = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An5);
        #endregion

        private string _filepath = "test.csv";
        public Sensors(string rootpath)
        {
            _filepath = Path.Combine(rootpath, _filepath);
        }

        public void HighSpeedRead()
        {
            while (FlightControl.CurrentPhase!=FlightPhases.Shutdown)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("{0},{1},{2},{3},{4},{5},{6}\r\n",
                    x18G.Read(),
                    y18G.Read(),
                    x5G.Read(),
                    y5G.Read(),
                    z250G.Read(),
                    z18G.Read(),
                    DateTime.Now.Ticks
                    );

                byte[] data = Encoding.UTF8.GetBytes(sb.ToString());
                File.WriteAllBytes(_filepath, data);
            }
        }
    }
}
